/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Util.LoginUtil;
import Model.User;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Daniel
 */
@ManagedBean(name = "loginBean")
public class LoginBean {

    private String username;
    private String password;
    public boolean render;
    public boolean admin;
    private String loginImage;

    public boolean isAdmin() {
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }
    
    

    public String getUsername() {
        return username;
    }

    public boolean isRender() {

        HttpSession session = (HttpSession) FacesContext
            .getCurrentInstance().getExternalContext().getSession(true);
        
             String user = (String) session.getAttribute("cod");
        if (user == null) {
            render = false;
            admin=false;
        } 
       
       else {
            if(user.equals("1"))
                admin=true;
                render = true;
        }

        return render;
    }

    public String isLogged() {
        HttpSession session = (HttpSession) FacesContext
            .getCurrentInstance().getExternalContext().getSession(true);
        String user = (String) session.getAttribute("cod");
        if (user == null) {
            loginImage = "/images/dock/login.png";
        } else {
            loginImage = "/images/dock/logout.png";
        }

        return loginImage;
    }

    public void setRender(boolean render) {
        this.render = render;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void confirmaLogin() {
        try {
            User us= new User();
            us.setEmail(username);
            us.setSenha(password);
            LoginUtil.confirmaLogin(us);
        } catch (Exception e) {
            System.out.println(e);
        }
        //   CadastroDao.getUser();
    }
    
    public void logout(){
        
        HttpSession session = (HttpSession) FacesContext
            .getCurrentInstance().getExternalContext().getSession(true);
        session.invalidate();
    }
}
